﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NENAData.Factory;
using NENAData.LINQ;

namespace NENAData.NENAMember.Referral
{
    class Referral
    {

        private static MembershipDataContext GetSQLTransaction()
        {
            var x = new SQLMembership();
            return x.GetMembershipDB();
        }

        public string GenerateAReferralCode()
        {
            MembershipDataContext db = GetSQLTransaction();
            Random random = new Random();
            int intCodeId = random.Next(0, 10000);

            var strCode = (from rcode in db.Referral_Codes
                              where rcode.ID == intCodeId
                           select rcode).FirstOrDefault();

            return strCode.Code;
        }

        public bool IsReferralCodeAlreadyUsed(string strReferralCode)
        {
            MembershipDataContext db = GetSQLTransaction();

            var oCount = (from oMember in db.Members
                          where oMember.Referral_Code == strReferralCode
                          select oMember).ToList();

            if (oCount.Count == 0)
                return false;
            else
                return true;

        }

    }
}
